package com.gitee.controller;

import com.gitee.bo.AdminInfoBo;
import com.gitee.bo.AdminPasswordBo;
import com.gitee.enums.ResponseEnum;
import com.gitee.enums.UploadTypeEnum;
import com.gitee.netty.NettyConfig;
import com.gitee.pojo.Admin;
import com.gitee.service.IAdminService;
import com.gitee.service.IPermissionService;
import com.gitee.vo.AdminVo;
import com.gitee.vo.PermissionVo;
import com.gitee.vo.ResponseVo;
import io.minio.MinioClient;
import io.minio.errors.*;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.UUID;

/**
 * 需要登录，但是不需要权限就能完成的功能
 */
@RestController
@RequestMapping("/auth")
public class AuthController {

    @Autowired
    private IPermissionService permissionService;

    @Autowired
    private IAdminService adminService;

    @Autowired
    private MinioClient minioClient;

    @Value("${minio.bucketName}")
    private String bucketName;

    /**
     * 获取菜单
     * @return
     */
    @GetMapping("/menu")
    public ResponseVo<List<PermissionVo>> menu() {
        AdminVo adminVo = (AdminVo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        List<PermissionVo> menuTree = permissionService.getMenuByUsername(adminVo.getUsername());
        return ResponseVo.ok(menuTree);
    }

    /**
     * 修改密码
     * @param adminPasswordBo
     * @return
     */
    @PutMapping("/password")
    public ResponseVo<String> password(@RequestBody AdminPasswordBo adminPasswordBo) {
        AdminVo adminVo = (AdminVo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        adminService.updateAdminPassword(adminPasswordBo, adminVo.getId());
        return ResponseVo.ok(ResponseEnum.UPDATE_SUCCESS);
    }

    /**
     * 查看个人信息
     * @return
     */
    @GetMapping("/info")
    public ResponseVo<Admin> info() {
        AdminVo adminVo = (AdminVo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Admin admin = adminService.getById(adminVo.getId());
        return ResponseVo.ok(admin);
    }

    /**
     * 更新个人信息
     * @param adminInfoBo
     * @return
     */
    @PutMapping("/info")
    public ResponseVo<String> updateInfo(@RequestBody AdminInfoBo adminInfoBo) {
        AdminVo adminVo = (AdminVo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        adminService.updateAdminInfo(adminInfoBo, adminVo.getId());
        return ResponseVo.ok(ResponseEnum.UPDATE_SUCCESS);
    }

    /**
     * 上传图片
     * code=1表示头像
     * code=2表示菜品图片
     * @param code
     * @return
     */
    @PostMapping("/upload")
    public ResponseVo<String> upload(MultipartFile file, Integer code) throws IOException, InvalidKeyException, NoSuchAlgorithmException, InsufficientDataException, InvalidArgumentException, InternalException, NoResponseException, InvalidBucketNameException, XmlPullParserException, ErrorResponseException {
        String originalFilename = file.getOriginalFilename();
        String filePath = UploadTypeEnum.of(code) + UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));
        minioClient.putObject(bucketName, filePath, file.getInputStream(), null);
        String httpPath = "/" + bucketName + filePath;
        return ResponseVo.ok(httpPath);
    }

}
